import com.sun.source.tree.Tree;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86138
 * Date: 2024-10-11
 * Time: 15:31
 */
public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    void preOrder(TreeNode root){
        if(root == null)return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        postOrder(root.right);
    }
    public List<Character> list = new LinkedList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if(root == null)return list;
        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return list;
    }

    void inOrder(TreeNode root){
        if(root == null)return;
        preOrder(root.left);
        System.out.print(root.val+" ");
        postOrder(root.right);
    }

    void postOrder(TreeNode root){
        if(root == null)return;
        preOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    // 获取树中节点的个数
    public static int nodeCount;
    public int size(TreeNode root) {
        if (root == null) return 0;
        nodeCount++;
        size(root.left);
        size(root.right);
        return nodeCount;
    }
    // 获取叶子节点的个数
    public static int leaveCount = 0;
    public int getLeafNodeCount(TreeNode root){
        if(root == null)return 0;
        if(root.left == null&& root.right == null){
            leaveCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leaveCount;
    }
    // 获取第K层节点的个数（左树k-1层+右树k-1层）
    int getKLevelNodeCount(TreeNode root,int k){
        if(root == null)return 0;
        if(k == 1)return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度(左树与右树高度的最大值加一)
    int getHeight(TreeNode root){
        if(root == null)return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight)+1;
    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val){
        if(root == null)return null;
        if(root.val ==val){
            return root;
        }
        TreeNode leftT = find(root.left,val);
        TreeNode rightT = find(root.right,val);
        if(leftT !=null)return leftT;
        if(rightT != null)return rightT;
        return null;
    }
}
